package com.banciyuan.bcywebview.base.activity;

import android.content.Context;
import android.os.Build;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.FrameLayout;

import com.baidu.mobstat.StatService;
import com.banciyuan.bcywebview.R;
import com.banciyuan.bcywebview.base.helper.data.UserDataHelper;
import com.banciyuan.bcywebview.utils.constants.MyConstants;
import com.banciyuan.bcywebview.utils.sp.SPConstant;
import com.banciyuan.bcywebview.utils.sp.SPHelper;
import com.banciyuan.bcywebview.utils.string.StringUtil;
import com.google.gson.Gson;
import com.umeng.message.PushAgent;
import com.umeng.message.UTrack;

import java.text.SimpleDateFormat;

import de.greenrobot.daoexample.model.NightMode;

//所有fragment的基类，为了规范activity的初始化流程
public class BaseActivity extends FragmentActivity {

    private boolean isShadowAdded;

    //获得intent返回的bundle中参数，和其他一些操作
    protected void initArgs() {
    }

    //初始化progressbar
    protected void initProgressbar() {
    }

    //初始化actionbar
    protected void initActionbar() {
    }

    //初始化ui组件
    protected void initUi() {
    }

    //初始化ui组件的监听
    protected void initAction() {
    }

    //初始化ui组件中的数据
    protected void initData() {
    }

    //neterror时的回调
    protected void onNetworkError(String result) {
    }

    protected Context getContext() {
        return this;
    }

    //为了调用百度统计
    @Override
    protected void onResume() {
        super.onResume();
        StatService.onResume(this);
        addShadow();
    }

    //为了调用百度统计
    @Override
    protected void onPause() {
        super.onPause();
        StatService.onPause(this);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        PushAgent.getInstance(this).onAppStart();
        UserDataHelper.saveInstance(this);
        reBindUmengAlias();
        changeStatusBar();
    }


    private boolean dealTime(Long now, NightMode nightMode) {
        SimpleDateFormat sdf = new SimpleDateFormat("HH:mm");
        String nowString = sdf.format(now);
        int nowInt = StringUtil.parseHourToInt(nowString);
        int startInt = StringUtil.parseHourToInt(nightMode.getStart_time());
        int endInt = StringUtil.parseHourToInt(nightMode.getEnd_time());
        if (endInt < startInt) {
            endInt += 24 * 100;
        }
        int during = endInt - startInt;
        if (nowInt >= startInt && nowInt - startInt <= during) {
            return true;
        } else {
            return false;
        }
    }

    protected void addShadow() {
        try {
            Gson gson = new Gson();
            NightMode nightMode = gson.fromJson(SPHelper.getString(this, SPConstant.SPNAME_CLOSELIGHT, SPConstant.All_LIGHT, "{}"), NightMode.class);
            if (nightMode.is_open() && dealTime(System.currentTimeMillis(), nightMode) && !isShadowAdded) {
                isShadowAdded = true;
                View rootView = findViewById(android.R.id.content);
                if (null == rootView) return;
                if (rootView instanceof FrameLayout) {
                    View shadowView = LayoutInflater.from(this).inflate(R.layout.shadow_layout, (FrameLayout) rootView, false);
                    ((FrameLayout) rootView).addView(shadowView);
                }
            } else if (nightMode.is_open() && !dealTime(System.currentTimeMillis(), nightMode) && isShadowAdded) {
                removeShadow();
            } else if (!nightMode.is_open() && isShadowAdded) {
                removeShadow();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    protected void removeShadow() {
        View rootView = findViewById(android.R.id.content);
        if (null == rootView) return;
        View view = rootView.findViewById(R.id.night_mode_shadow);
        if (null != view) {
            ((ViewGroup) rootView).removeView(view);
            isShadowAdded = false;
        }
    }

    private void reBindUmengAlias() {
        PushAgent mPushAgent = PushAgent.getInstance(this);
        if (!SPHelper.getBoolean(this, SPConstant.SETALIASSUCC, false)) {
            String uid = UserDataHelper.getInstance(this).getUid();
            if (!TextUtils.isEmpty(uid) && !TextUtils.isEmpty(SPHelper.getString(this, SPConstant.SPNAME_DEVICES, SPConstant.TOKEN, ""))) {
                mPushAgent.addAlias(uid, MyConstants.BCYUSER, new UTrack.ICallBack() {
                    @Override
                    public void onMessage(boolean b, String s) {
                        SPHelper.putBoolean(BaseActivity.this, SPConstant.SETALIASSUCC, b);
                    }
                });
            }
        }
    }

    protected void immersive(){
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
        }
    }

    protected void hideStatus(){
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
        }
    }

    protected void changeStatusBar() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            Window window = getWindow();
            window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
            window.setStatusBarColor(getResources().getColor(R.color.black));
        }
    }
}